package org.hot100_5.leetcode128;

import java.util.HashSet;

public class Solution {
    public int longestConsecutive(int[] nums) {

        HashSet<Integer> hashSet = new HashSet<>();

        for (int num :nums) {
            hashSet.add(num);
        }

        int n = nums.length;
        int maxLength = 0;
        for (int i = 0; i < n; i++) {
            int currentLength = 1;
            int currNum = nums[i];
            if (!hashSet.contains(currNum-1)){
                currNum++;
                currentLength++;
                while (hashSet.contains(currNum)){
                    currentLength++;
                    currNum++;
                }
                maxLength=Math.max(maxLength,currentLength);
            }
        }

        return maxLength;
    }
}
